//
//  SPBluetoothlistPopView.m
//  Spinning
//
//  Created by Neil on 2018/1/26.
//  Copyright © 2018年 com. All rights reserved.
//

#import "SPBluetoothListPopView.h"
#import <BabyBluetooth/BabyBluetooth.h>
#import "BluetoothListCell.h"

NSString *cellIdentifier = @"bluetoothListCellIdentifier";
@interface SPBluetoothListPopView () <UITableViewDelegate, UITableViewDataSource, BluetoothListCellDelegate>
@property (nonatomic, strong) UIButton *btnCancel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *scanBtn;

@end

@implementation SPBluetoothListPopView 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    self.titleLabel = [UILabel labelWithframe:CGRectZero text:@"请选择设备" font:[UIFont systemFontOfSize:17] textAlignment:NSTextAlignmentCenter textColor:@"#EF9B39"];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.titleLabel];
    
//    self.scanBtn = [[UIButton alloc] init];
//    [self.scanBtn setImage:[UIImage imageNamed:@"扫码"] forState:UIControlStateNormal];
//    [self addSubview:self.scanBtn];
//    [self.scanBtn addTarget:self action:@selector(scanPeriphealAction) forControlEvents:UIControlEventTouchUpInside];
    
    _listTableView = [[UITableView alloc] init];
    [self addSubview:_listTableView];
//    [_listTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
    [_listTableView registerClass:[BluetoothListCell class] forCellReuseIdentifier:cellIdentifier];

    _listTableView.rowHeight = 44.0f;
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    
    
    self.btnCancel = [UIButton buttonWithframe:CGRectZero text:@"取消" font:[UIFont systemFontOfSize:17] textColor:@"#EF9B39" backgroundColor:nil normal:nil selected:nil];
    [self.btnCancel addTarget:self action:@selector(actionHide) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.btnCancel];
    
    
//    CGFloat alertWidth = 280.0f * scaleX;
//    CGFloat alertHeight = 256.0f * scaleX;
//    
//    self.frame = CGRectMake(0, 0, alertWidth, alertHeight);
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 5.0f;
    self.layer.masksToBounds = YES;


    self.titleLabel.frame = CGRectMake(0, 0, self.width, 40);

    
//    [self.scanBtn makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.mas_right).offset(-10);
//        make.centerY.equalTo(self.titleLabel.centerY);
//        make.height.equalTo(40);
//        make.width.equalTo(40);
//    }];
    
    self.btnCancel.frame = CGRectMake(0, self.height-40, self.width, 40);

    self.listTableView.frame = CGRectMake(0, self.titleLabel.bottom, self.width, self.btnCancel.top-self.titleLabel.bottom);

    
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)hide
{
    [self removeFromSuperview];
}

#pragma mark - table view delegate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.periphealArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
//    CBPeripheral *peripheral =  (CBPeripheral *)self.periphealArray[indexPath.row];
//    cell.textLabel.text = peripheral.name;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BluetoothListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NSString *name = self.periphealArray[indexPath.row];
    cell.nameLabel.text = name;
    cell.row = indexPath.row;
    cell.delegate = self;
    
    if ([name isEqualToString:self.per.name]) {
        cell.breakBtn.hidden = NO;
    }
    else {
        cell.breakBtn.hidden = YES;

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(connectInListToPeripheral:row:)]) {
        [self.delegate connectInListToPeripheral:self.periphealArray[indexPath.row] row:indexPath.row];
    }
}

- (void)scanPeriphealAction
{
    if ([self.delegate respondsToSelector:@selector(scanAction)]) {
        [self.delegate scanAction];
    }
}

#pragma mark - button method
- (void)actionHide {
    [self hide];
}

#pragma mark - setter and getter
- (NSMutableArray *)periphealArray {
    if (!_periphealArray) {
        _periphealArray = [NSMutableArray array];
    }
    return _periphealArray;
}

- (NSMutableArray *)rememberArray {
    if (!_rememberArray) {
        _rememberArray = [NSMutableArray array];
    }
    return _rememberArray;
}

#pragma mark - BluetoothListCellDelegate
- (void)breakBtnResponderWithRow:(NSInteger)row {
    if ([self.delegate respondsToSelector:@selector(breakBtnResponderByTableViewWithRow:)]) {
        [self.delegate breakBtnResponderByTableViewWithRow:row];
    }
}

@end
